package com.baosight.sczx.zm.cp.service;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.Department;
import com.baosight.sczx.common.zm.cInterface.cp.ProducePlan;
import com.baosight.sczx.common.zm.cInterface.cp.ProfessionalPlan;
import com.baosight.sczx.common.zm.cInterface.cs.SystemSettingCP;
import com.baosight.sczx.zm.cp.domain.ZMCPProducePlanYT;

/**
 * <p>
 * Title: ServiceZMCP31.java
 * </p>
 * <p>
 * Description: 一通三防专业计划明细
 * </p>
 * <p>
 * Copyright: Baosight Software LTD.co Copyright (c) 2002
 * </p>
 * <p>
 * Company: Baosight Software
 * </p>
 * 
 * @author 张坤
 * @version 1.0
 * 
 *          <pre>
 * Histroy:
 *          2016-03-21 下午02:51:04  张坤  Create
 *          </pre>
 */
public class ServiceZMCP31 extends ServiceEPBase {
	/**
	 * <p>
	 * Function: initLoad
	 * </p>
	 * <p>
	 * Description: 初始化
	 * </p>
	 * <p>
	 * return value: ServiceZMCP02
	 * </p>
	 * <p>
	 * throws Exception:
	 * </p>
	 * <p>
	 * History: 2016-03-21下午02:51:38 张坤
	 * </p>
	 */
	@Override
	public EiInfo initLoad(EiInfo inInfo) {
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");// 如果不加这个，翻页的统计和显示是错误的
		EiInfo outInfo = this.query(inInfo);
		return outInfo;
	}

	/**
	 * <p>
	 * Function: query
	 * </p>
	 * <p>
	 * Description: 查询
	 * </p>
	 * <p>
	 * return value: ServiceZMCP02
	 * </p>
	 * <p>
	 * throws Exception:
	 * </p>
	 * <p>
	 * History: 2016-03-21下午02:51:58 张坤
	 * </p>
	 *
	 */
	@Override
	public EiInfo query(EiInfo inInfo) {
		String fdProfessionalplanId = null;
		String fdProduceplanId = null;

		if (inInfo.getString("fdProfessionalplanId") != null)
			fdProfessionalplanId = inInfo.getString("fdProfessionalplanId");
		StaticTools.setQueryParameters(inInfo, "fdProfessionalplanId", fdProfessionalplanId, true);// 添加查询条件
		if (inInfo.getString("fdProduceplanId") != null)
			fdProduceplanId = inInfo.getString("fdProduceplanId");
		StaticTools.setQueryParameters(inInfo, "fdProduceplanId", fdProduceplanId, true);// 添加查询条件

		EiInfo outInfo = inInfo;
		if ((fdProfessionalplanId != null && !fdProfessionalplanId.equals(""))
				|| (fdProduceplanId != null && !fdProduceplanId.equals("")))
			outInfo = super.query(inInfo, "ZMCPProducePlanYT.query", new ZMCPProducePlanYT());
		else
			outInfo.addBlock("result").setBlockMeta(new ZMCPProducePlanYT().eiMetadata);

		outInfo.set("fdCountBeginDate", SystemSettingCP.getInstance().getBeginDate());
		outInfo.addBlock(Department.getInstance().getMiningDepts("cmes.cp.department", "orgCode", "orgName", false,
				UserSession.getInSessionProperty("miningCode").toString()));
		return outInfo;
	}

	/**
	 * <p>
	 * Function: insert
	 * </p>
	 * <p>
	 * Description: 新增
	 * </p>
	 * <p>
	 * return value: ServiceZMCP02
	 * </p>
	 * <p>
	 * throws Exception:
	 * </p>
	 * <p>
	 * History: 2016-03-21下午03:07:57 张坤
	 * </p>
	 *
	 */
	@Override
	public EiInfo insert(EiInfo inInfo) {
		// 校验专业计划或生产计划是否已上报,tempInfo.getStatus()为2表示计划已上报
		inInfo = isNotReport(inInfo);
		if (inInfo.getStatus() == 2) {
			inInfo = this.query(inInfo);
			return inInfo;
		}

		String fdPlanYear = inInfo.getString("fdPlanYear");
		String fdPlanMonth = inInfo.getString("fdPlanMonth");
		EiInfo out = new EiInfo();
		ZMCPProducePlanYT _dom = new ZMCPProducePlanYT();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			_dom.setFdCreateDepartmentId(UserSession.getInSessionProperty("orgCode").toString());
			_dom.setFdCreateDepartment(UserSession.getInSessionProperty("orgName").toString());
			_dom.setFdDepartment(Department.getInstance().getDepartmentNameById(_dom.getFdDepartmentId()));
			_dom.setFdXzdepartment(Department.getInstance().getDepartmentNameById(_dom.getFdXzdepartmentId()));
			_dom.setFdCreatorId(UserSession.getUserId().toString());
			_dom.setFdCreator(UserSession.getInSessionProperty("UserCname").toString());// 中文名
			if (inInfo.getString("fdProfessionalplanId") != null) {
				_dom.setFdProfessionalplanId(inInfo.getString("fdProfessionalplanId"));
			} else if (inInfo.getString("fdProduceplanId") != null) {
				_dom.setFdProduceplanId(inInfo.getString("fdProduceplanId"));
			}
			_dom.setFdKeyId(TablePk.getInstance().getNextValue("T_CP_PRODUCEPLAN_YT"));
			_dom.setFdPlanYear(fdPlanYear);
			_dom.setFdPlanMonth(fdPlanMonth);
			this.dao.insert("ZMCPProducePlanYT.insert", _dom);
		}
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out;
	}

	/**
	 * <p>
	 * Function: update
	 * </p>
	 * <p>
	 * Description: 修改
	 * </p>
	 * <p>
	 * return value: ServiceZMCP02
	 * </p>
	 * <p>
	 * throws Exception:
	 * </p>
	 * <p>
	 * History: 2016-03-21下午04:20:50 张坤
	 * </p>
	 *
	 */
	@Override
	public EiInfo update(EiInfo inInfo) {
		// 校验专业计划或生产计划是否已上报,tempInfo.getStatus()为2表示计划已上报
		inInfo = isNotReport(inInfo);
		if (inInfo.getStatus() == 2) {
			inInfo = this.query(inInfo);
			return inInfo;
		}
		EiInfo out = new EiInfo();
		ZMCPProducePlanYT _dom = new ZMCPProducePlanYT();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			_dom.setFdCreateDepartmentId(UserSession.getInSessionProperty("orgCode").toString());
			_dom.setFdCreateDepartment(UserSession.getInSessionProperty("orgName").toString());
			_dom.setFdDepartment(Department.getInstance().getDepartmentNameById(_dom.getFdDepartmentId()));
			_dom.setFdXzdepartment(Department.getInstance().getDepartmentNameById(_dom.getFdXzdepartmentId()));
			_dom.setFdCreatorId(UserSession.getUserId().toString());
			_dom.setFdCreator(UserSession.getInSessionProperty("UserCname").toString());// 中文名
			this.dao.update("ZMCPProducePlanYT.update", _dom);
		}
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out;

	}

	/**
	 * <p>
	 * Function: delete
	 * </p>
	 * <p>
	 * Description: 删除
	 * </p>
	 * <p>
	 * return value: ServiceZMCP02
	 * </p>
	 * <p>
	 * throws Exception:
	 * </p>
	 * <p>
	 * History: 2016-03-21下午04:30:17 张坤
	 * </p>
	 *
	 */
	@Override
	public EiInfo delete(EiInfo inInfo) {
		// 校验专业计划或生产计划是否已上报,tempInfo.getStatus()为2表示计划已上报
		inInfo = isNotReport(inInfo);
		if (inInfo.getStatus() == 2) {
			inInfo = this.query(inInfo);
			return inInfo;
		}
		EiInfo _temp = super.delete(inInfo, "ZMCPProducePlanYT.delete");
		EiInfo outInfo = this.query(inInfo);
		outInfo.setMsg(_temp.getMsg());
		outInfo.setDetailMsg(_temp.getDetailMsg());
		return outInfo;
	}

	/**
	 * 判断专项计划或生产计划是否未上报,true表示计划未上报,false表示计划已上报
	 */
	public EiInfo isNotReport(EiInfo inInfo) {
		boolean isNotReport = false;
		if (inInfo.getString("fdProfessionalplanId") != null) {// 专业计划
			isNotReport = ProfessionalPlan.validate(inInfo.getString("fdProfessionalplanId"));
			if (isNotReport == false) {
				inInfo.setStatus(2);
				inInfo.setMsg("当月的专业计划已经上报或作废，不能操作！");
			}
		} else if (inInfo.getString("fdProduceplanId") != null) {// 生产计划
			isNotReport = ProducePlan.validate(inInfo.getString("fdProduceplanId"));
			if (isNotReport == false) {
				inInfo.setStatus(2);
				inInfo.setMsg("当月的生产计划已经上报或作废，不能操作！");
			}
		}
		return inInfo;
	}
}